<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/18
 * Time: 18:09
 */
require('env.php');
require('init.inc.php');
require('order_store_procedure.php');


$action = $_GET['action'];
if ($action == 'shop_search') {
    $search = $_GET['order_search'];
    if (!$search) {
        $order = array();
    }

    if (preg_match("/[\x7f-\xff]/", $search)) {
        $condition = " WHERE  user_name LIKE '%$search%' ";//名字採用模糊查詢
    } elseif (is_numeric($search)) {
        $condition = " WHERE  phone = '$search' ";
    } else {
        $condition = " WHERE  order_sn = '$search' ";
    }

//    $order = Order::getByCondition($condition);

    $order_num = '';
    $order = OrderGetByCondition($condition);
    //跨多个服务器，所以得带有服务器标识
    foreach ($order as $website => $v) {
        if ($v[0]) {
            $order_num = $v[0]['order_sn'];
//        $orderP = OrderProduct::getProdsBySn($order['order_sn']);
            $orderP = getShopCartBySn($v[0]['order_sn'], $website);
            foreach ($orderP as $ord) {
//            $product = Products::getProductById($ord['product_id']);
                $product = getProductById($ord['product_id'], $website);

                $new_order['xiangmu'] = $product['products_name'];
                $new_order['price'] = $product['products_special_price'];
                $new_order['nums'] = $ord['num'];
                $orderProds[] = $new_order;
                $totalPrice += ($product['products_special_price'] * $ord['num']);
            }

            $nn_order['website'] = $website;
            $nn_order['username'] = $v[0]["user_name"];
            $nn_order['phone'] = $v[0]["phone"];
            $sm = Utils::getValue($v[0]['shipping_method'], 'shipping_method');
            $nn_order['ysfs'] = $sm;
            $st = Utils::getValue($v[0]['shipping_time'], 'shipping_time');
            $nn_order['yssj'] = $st;

            $fkfs = Utils::getValue($v[0]['pay_type'], 'pay_method');
            $nn_order['fkfs'] = $fkfs;
            $nn_order['addres'] = $v[0]["addres"];
            $nn_order['email'] = $v[0]["email"];
            $nn_order['line'] = $v[0]["line"];
            $nn_order['note'] = $v[0]["note"];
            $nn_order['status'] = $v[0]["status"];

            $kdinfo = getKdInfoBySn($v[0]['order_sn'], $website);

            if ($kdinfo["isGo"] == 0) {
                $nn_order['is_fh'] = "未發貨";
                $nn_order['kd_status'] = "";
            } elseif ($kdinfo["isGo"] == 1) {
                $nn_order['is_fh'] = "已發貨";
                $kd_status = getheimaoStatusByAPI($kdinfo["kd_sn"]);
                $nn_order['kd_status'] = $kd_status;
            } else {
                $nn_order['is_fh'] = "取消訂單";
                $nn_order['kd_status'] = "";
            }

            $nn_order['kd_name'] = $kdinfo["kd_name"];
            $nn_order['kd_num'] = $kdinfo["kd_sn"];
            $nn_order['kd_url'] = $kdinfo["kd_url"];

            $nn_order['totalPrice'] = $totalPrice;
            $nn_order['productsinfo'] = $orderProds;
            $toal_orders[] = $nn_order;
        }
    }

} else {
    $order_sn = $_GET['order_sn'];
    $to_email = '';
    $productsInfo = '';
    $condition = " WHERE  order_sn = '$order_sn' ";
//    $orderP = OrderProduct::getProdsBySn($order_sn);
//    $totalPrice = 0;
//    $orderProds = array();
//    foreach ($orderP as $ord) {
//        $product = Products::getProductById($ord['product_id']);
//        $new_order['xiangmu'] = $product['products_name'];
//        $new_order['price'] = $product['products_special_price'];
//        $new_order['nums'] = $ord['product_nums'];
//        $orderProds[] = $new_order;
//        $totalPrice += ($product['products_special_price'] * $ord['product_nums']);
//    }
//
//    $order = Order::getByOrderSn($order_sn);


    $order_num = '';
    $order = OrderGetByCondition($condition);
    //跨多个服务器，所以得带有服务器标识
    foreach ($order as $website => $v) {
        if ($v[0]) {
            $order_num = $v[0]['order_sn'];
//        $orderP = OrderProduct::getProdsBySn($order['order_sn']);
            $orderP = getShopCartBySn($v[0]['order_sn'], $website);
            foreach ($orderP as $ord) {
//            $product = Products::getProductById($ord['product_id']);
                $product = getProductById($ord['product_id'], $website);
                $new_order['xiangmu'] = $product['products_name'];
                $new_order['price'] = $product['products_special_price'];
                $new_order['nums'] = $ord['num'];
                $orderProds[] = $new_order;

                $new_order_db['xiangmu'] = urlencode($product['products_name']);
                $new_order_db['price'] = $product['products_special_price'];
                $new_order_db['nums'] = $ord['num'];
                $orderDB[] = $new_order_db;
                $productsInfo = json_encode($orderDB);
                $totalPrice += ($product['products_special_price'] * $ord['num']);
            }

            $nn_order['website'] = $website;
            $nn_order['username'] = $v[0]["user_name"];
            $nn_order['phone'] = $v[0]["phone"];
            $sm = Utils::getValue($v[0]['shipping_method'], 'shipping_method');
            $nn_order['ysfs'] = $sm;
            $st = Utils::getValue($v[0]['shipping_time'], 'shipping_time');
            $nn_order['yssj'] = $st;

            $fkfs = Utils::getValue($v[0]['pay_type'], 'pay_method');
            $nn_order['fkfs'] = $fkfs;
            $nn_order['addres'] = $v[0]["addres"];
            $nn_order['email'] = $v[0]["email"];
            $to_email = $nn_order['email'];
            $nn_order['line'] = $v[0]["line"];
            $nn_order['note'] = $v[0]["note"];
            $nn_order['status'] = $v[0]["status"];

            $kdinfo = getKdInfoBySn($v[0]['order_sn'], $website);

            if ($kdinfo["isGo"] == 0) {
                $nn_order['is_fh'] = "未發貨";
                $nn_order['kd_status'] = "";
            } elseif ($kdinfo["isGo"] == 1) {
                $nn_order['is_fh'] = "已發貨";
                $kd_status = getheimaoStatusByAPI($kdinfo["kd_sn"]);
                $nn_order['kd_status'] = $kd_status;
            } else {
                $nn_order['is_fh'] = "取消訂單";
                $nn_order['kd_status'] = "";
            }

            $nn_order['kd_name'] = $kdinfo["kd_name"];
            $nn_order['kd_num'] = $kdinfo["kd_sn"];
            $nn_order['kd_url'] = $kdinfo["kd_url"];


            $nn_order['totalPrice'] = $totalPrice;
            $nn_order['productsinfo'] = $orderProds;

            $toal_orders[] = $nn_order;
        }
    }

    //更新數據庫狀態
    changeOrderSnStatus($order_sn);
    $mail_data = array("to_email" => $to_email, "order_sn" => $order_sn, "products_info" => $productsInfo, "createtime" => time());
    saveEmailData($mail_data);
}

$tongji1 = Tongji::getTongji(1);
$tongji2 = Tongji::getTongji(2);
$tongji3 = Tongji::getTongji(3);

$online_service = OnService::online_service();

$index_std = ShopConfig::getConfig();


?>

<!DOCTYPE html>
<html>
<?php include("header.php"); ?>

<style>
    .fb_chat {
        background-image: url(style/images/fbc.png);
        background-repeat: no-repeat;
        background-size: auto;
        background-position: 0 0px;
        margin-left: 50%;
        display: block;
        height: 40px;
        outline: none;
        overflow: hidden;
        white-space: nowrap;
        width: 40px;
    }

    .toolbar {
        text-align: center;
    }

    .btn-buy {
        background: #FF6F20;
        background-size: 15px;
        width: 100px;
        height: 50px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        font-weight: bold;
        margin-left: 45%;
</style>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h3 class="center">
                訂單編號: <?php echo $order_num ?>
                <br>
                <small class="font-danger">請截圖或收藏網址以便日後查詢。</small>
            </h3>
            <hr>
            <div class="alert alert-danger">
                <p>★ 如果有任何問題可以點選在綫客服哦
                    <br/>★ 我們採用專業宅配，到時再把錢拿給貨運司機就可以了哦。如果臨時不在可以等貨運司機打電話給你再改約收件時間即可</p>

                <p>【請收到貨品後若有問題請盡快聯絡客服，我們可以立即幫你處理哦】</p>
            </div>
            <hr>

            <h5 class="visible-xs font-danger center">可左右拖曳查看購物明細</h5>


            <?php if ($toal_orders) { ?>
                <?php foreach ($toal_orders as $order_item) { ?>
                    <div style="border: 1px solid red;margin-top: 20px;">
                        <div class="table-responsive">
                            <h1>购买站点：<?php echo $order_item["website"]; ?></h1>
                            <table class="table">
                                <tr>
                                    <th class="center success" colspan="4">商品明細</th>
                                </tr>
                                <tr>
                                    <th>品項</th>
                                    <th class="right" width="100">單價</th>
                                    <th class="right" width="100">數量</th>
                                    <th class="right" width="100">總計</th>
                                </tr>
                                <?php foreach ($order_item['productsinfo'] as $o) { ?>
                                    <tr>
                                        <td>
                                            <?php echo $o['xiangmu'] ?>

                                        </td>
                                        <td class="right">
                                            <?php echo $o['price'] ?>
                                        </td>
                                        <td class="right">
                                            <?php echo $o['nums'] ?>
                                        </td>
                                        <td class="right"> <?php echo($o['price'] * $o['nums']) ?></td>
                                    </tr>
                                <?php } ?>
                                <tr>
                                    <th class="right" colspan="3">運費</th>
                                    <td class="right">
                                        <span class="font-danger">免運</span>
                                    </td>
                                </tr>
                                <tr>
                                    <th class="right" colspan="3">總計</th>
                                    <td class="right">
                                        <strong> <?php echo $order_item['totalPrice'] ?></strong>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="right">
                                        <strong></strong>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <table class="table">
                            <tr>
                                <th class="center success" colspan="2">收件詳情</th>
                            </tr>
                            <tr>
                                <th width="120">收件人</th>
                                <td>
                                    <?php echo $order_item['username'] ?>
                                    <small></small>
                                </td>
                            </tr>
                            <tr>
                                <th>聯絡電話</th>
                                <td><?php echo $order_item['phone'] ?></td>
                            </tr>
                            <tr>
                                <th>聯絡邮箱</th>
                                <td><?php echo $order_item['email'] ?></td>
                            </tr>
                            <tr>
                                <th>line號碼</th>
                                <td><?php echo $order_item['line'] ?></td>
                            </tr>
                            <tr>
                                <th>運送方式</th>
                                <td><?php
                                    echo $order_item['ysfs']; ?></td>
                            </tr>
                            <tr>
                                <th>付款方式</th>
                                <td><?php
                                    echo $order_item['fkfs']; ?></td>
                            </tr>
                            <tr>
                                <th>運送至地址</th>
                                <td><?php echo $order_item['addres'] ?></td>
                            </tr>
                            <tr>
                                <th>指定時間</th>
                                <td><?php
                                    echo $order_item['yssj']; ?></td>
                            </tr>
                            <tr>
                                <th>訂單留言</th>
                                <td><?php echo $order_item['note'] ?></td>
                            </tr>
                            <tr>
                                <th>訂單狀態</th>
                                <td><?php if ($order_item['status']) {
                                        echo '<span style="color: red;font-size: 20px;">未完成</span>';
                                    } else {
                                        echo '<span style="color: green;font-size: 20px;">已完成</span>';
                                    } ?></td>
                            </tr>
                        </table>

                        <table class="table">
                            <tr>
                                <th class="center success" colspan="2">物流詳情</th>
                            </tr>
                            <tr>
                                <th width="120">是否發貨</th>
                                <td style="color: red;font-size: 20px;">
                                    <?php echo $order_item['is_fh'] ?>
                                    <small></small>
                                </td>
                            </tr>
                            <tr>
                                <th>快遞公司</th>
                                <td><?php echo $order_item['kd_name'] ?></td>
                            </tr>
                            <tr>
                                <th>快遞官網</th>
                                <td><?php echo $order_item['kd_url'] ?></td>
                            </tr>
                            <tr>
                                <th>快遞單號</th>
                                <td><?php echo $order_item['kd_num'] ?></td>
                            </tr>
                            <tr>
                                <th>快遞狀態</th>
                                <td><?php echo $order_item['kd_status'] ?></td>
                            </tr>
                        </table>
                        <h4 class="center">貨物到達時間為5-7個工作日,有問題請諮詢客服</h4>
                    </div>
                <?php } ?>
            <?php } else { ?>
                <h3 style="text-align: center;color: blue;font-size: 35px;">無訂單數據！</h3>
            <?php } ?>
        </div>
    </div>
</div>
<h3 class="center">
    如有相關訂單問題
    <br>
    <small class="font-primary">請聯繫客服人員並且提供訂單編號。</small>
</h3>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="push-xs"></div>
            <h3 class="center">如有任何問題，請複製以下郵箱聯繫客服</h3>
            <h4 class="center"><?php
                preg_match_all('/<a .*?href="(.*?)".*?>*<\/a>/is', $index_std['store_footer'], $footer);
                echo $footer[0][0];
                //
                //                $len = strlen(substr($index_std['store_footer'], strpos($index_std['store_footer'], '</a>'), strlen($index_std['store_footer']) - strpos($index_std['store_footer'], '</a>') + 1));
                //                $str = substr($index_std['store_footer'], strpos($index_std['store_footer'], '<a>'), strlen($index_std['store_footer']) - strpos($index_std['store_footer'], '<a>') + 1 - $len);
                //                echo $str;
                ?></h4>

            <div class="push-xs"></div>
        </div>
    </div>
</div>


<div style="display:none;">
    <?php echo str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji1['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji2['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji3['tongji'], ENT_QUOTES)) . '<br/>'; ?>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <hr>
            <a class="btn btn-lg btn-default btn-block" href="<?php echo SERVER_ROOT ?>">回商城首頁繼續購物</a>
        </div>
    </div>
</div>

<div class="push-lg"></div>
<div id="modal-products"></div>
</body>
</html>